package com.mvp.lt.airlineview.kml;

import com.mvp.lt.airlineview.kml.entity.KmlLine;
import com.mvp.lt.airlineview.kml.entity.KmlPoint;
import com.mvp.lt.airlineview.kml.entity.KmlPolygon;
import com.mvp.lt.airlineview.kml.entity.KmlProperty;
import com.mvp.lt.airlineview.kml.utils.FileEndCodeUtils;
import com.mvp.lt.airlineview.kml.utils.ParsingKmlUtil;
import com.mvp.lt.airlineview.kml.utils.UnicodeInputStream;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;

/**
 * Created by ${LiuTao}.
 * User: Administrator
 * Name: AirLineView
 * functiona:
 * Date: 2019/7/2 0002
 * Time: 上午 8:44
 */
public class Main {
    public static void main(String[] args) throws FileNotFoundException {


        long starttime = System.currentTimeMillis();
        ParsingKmlUtil parsingKmlUtil = new ParsingKmlUtil();
//            File file = new File("D:/DJI/AirLineView/app/kml/永定增减挂验收.kml");
//                        File file1 = new File("D:/DJI/AirLineView/app/kml/1.kml");
//        File file2 = new File("D:/DJI/AirLineView/app/kml/2.kml");
//            File file = new File("D:/DJI/AirLineView/app/kml/point.kml");
        File targetFile = new File("D:/DJI/AirLineView/app/kml/2222.kml");

//            File file = new File("D:/DJI/AirLineView/app/kml/我的地点.kml");
//            File file = new File("D:/DJI/AirLineView/app/kml/永定增减挂验收.kml");
//        File file1 = new File("D:/DJI/AirLineView/app/kml/1.kml");
        File file2 = new File("D:/DJI/AirLineView/app/kml/2.kml");
//            File file = new File("D:/DJI/AirLineView/app/kml/永定增减挂验收3.kml");

        try {
//            FileEndCodeUtils.turnUTF8withoutBOM(file2, targetFile);
//            FileInputStream in1 = new FileInputStream(file1);
//            String code1 = getTxtEncode(in1);
//            System.out.println(code1);

            getKMLData(parsingKmlUtil, file2);
            long endtime = System.currentTimeMillis();
            long time = endtime - starttime;

            System.out.println(time + "ms");
        } catch (Exception e) {
            try {
                FileEndCodeUtils.turnUTF8withoutBOM(file2, targetFile);
                getKMLData(parsingKmlUtil, targetFile);
            } catch (IOException e1) {
                e1.printStackTrace();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
//            e.printStackTrace();
        }
    }

    private static void getKMLData(ParsingKmlUtil parsingKmlUtil, File targetFile) throws Exception {

        KmlProperty kmlProperty = parsingKmlUtil.parseKmlForJAK(targetFile);

        if (kmlProperty.getKmlPoints().size() > 0) {
            for (KmlPoint k : kmlProperty.getKmlPoints()) {
                System.out.println("点：" + k.getName());
            }
//                System.out.println("点");
        }
        if (kmlProperty.getKmlLines().size() > 0) {
            for (KmlLine k : kmlProperty.getKmlLines()) {
                System.out.println("线:" + k.getName());
            }
//                System.out.println("线");
        }
        if (kmlProperty.getKmlPolygons().size() > 0) {
            for (KmlPolygon k : kmlProperty.getKmlPolygons()) {
                System.out.println("面:" + k.getPoints());
            }
//                System.out.println("面");
        }
    }


    public static String getTxtEncode(FileInputStream in) throws IOException {
        String dc = Charset.defaultCharset().name();
        UnicodeInputStream uin = new UnicodeInputStream(in, dc);

        if ("UTF-8".equals(uin.getEncoding())) {
            uin.close();
            return "UTF-8";
        }
        uin.close();

        byte[] head = new byte[3];
        in.read(head);
        String code = "GBK";
        if (head[0] == -1 && head[1] == -2)
            code = "UTF-16";
        if (head[0] == -2 && head[1] == -1)
            code = "Unicode";
        //带BOM
        if (head[0] == -17 && head[1] == -69 && head[2] == -65)
            code = "带BOM UTF-8";
        if ("Unicode".equals(code)) {
            code = "UTF-16";
        }
        return code;
    }

    /**
     * 读取文件内容
     *
     * @return
     */
    public static String readtext(File f) {
        if (!f.exists()) {
            return null;
        }
        FileInputStream is;
        String result = null;
        try {
            is = new FileInputStream(f);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] array = new byte[1024];
            int len = -1;
            while ((len = is.read(array)) > 0 - 1) {
                System.out.println("行：" + len);
                if (len == 1) {

                } else {
                    bos.write(array, 0, len);
                }
            }
            byte[] data = bos.toByteArray(); // 取内存中保存的数据
            result = new String(data, "UTF-8");
            bos.close();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}
